const state = {
  realTimeAlarms: [],  // [{alarm:alarm, flag:false},...]
  realTimeEvents: [],
}

const mutations = {
  ADD_ALARM: (state, arr) => {
    var oldrealTimeAlarms = state.realTimeAlarms
    state.realTimeAlarms = []
    for (let i = 0; i < arr.length; i++) {
      let a = arr[i]
      state.realTimeAlarms.push({alarm: a, flag: false})
    }
    for (let i = 0; i < state.realTimeAlarms.length; i++) {
      let a = state.realTimeAlarms[i].alarm
      for (let j = 0; j < oldrealTimeAlarms.length; j++) {
        let alm = oldrealTimeAlarms[j].alarm
        if (alm.happenTime === a.happenTime && alm.alarmContent === a.alarmContent &&
          alm.stationName === a.stationName && alm.trainNo === a.trainNo && 
          alm.workNo === a.workNo && alm.djNo === a.djNo) {
            state.realTimeAlarms[i].speakNumber = oldrealTimeAlarms[j].speakNumber
        }
      }
    }
    state.realTimeAlarms.sort((a, b) => {
      if (a.alarm.happenTime === b.alarm.happenTime)
          return a.alarm.alarmContent.localeCompare(b.alarm.alarmContent)
        else 
          return Date.parse(a.alarm.happenTime) - Date.parse(b.alarm.happenTime)
    })
  
    while (state.realTimeAlarms.length > 99) {
      state.realTimeAlarms.shift()
    }

    let currentDt = new Date()
    for (let i = 0; i < state.realTimeAlarms.length; i++) {
      let alm = state.realTimeAlarms[i].alarm
      let diff = currentDt - Date.parse(alm.happenTime)
      if (diff > 24 * 3600000) {
        state.realTimeAlarms.shift()
      }
      else {
        break
      }
    }
    state.realTimeAlarms.reverse()
  },
  ADD_EVENT: (state, arr) => {
    for (let i = 0; i < arr.length; i++) {
      let a = arr[i]
      let isExist = false
      for (let i = 0; i < state.realTimeEvents.length; i++) {
        let alm = state.realTimeEvents[i].alarm
        if (alm.happenTime === a.happenTime && alm.alarmContent === a.alarmContent &&
          alm.stationName === a.stationName && alm.trainNo === a.trainNo && 
          alm.workNo === a.workNo && alm.djNo === a.djNo) {
            isExist = true
            break
        }
      }
  
      if (!isExist) {
        state.realTimeEvents.push({alarm: a, flag: false})
      }
    }

    state.realTimeEvents.sort((a, b) => {
      if (a.alarm.happenTime === b.alarm.happenTime)
          return a.alarm.alarmContent.localeCompare(b.alarm.alarmContent)
        else 
          return Date.parse(a.alarm.happenTime) - Date.parse(b.alarm.happenTime)
    })
  
    while (state.realTimeEvents.length > 99) {
      state.realTimeEvents.shift()
    }

    let currentDt = new Date()
    for (let i = 0; i < state.realTimeEvents.length; i++) {
      let alm = state.realTimeEvents[i].alarm
      let diff = currentDt - Date.parse(alm.happenTime)
      if (diff > 24 * 3600000) {
        state.realTimeEvents.shift()
      }
      else {
        break
      }
    }
    state.realTimeEvents.reverse()
  },
  SET_ALARM_FLAG: (state, a) => {
    for (let i = 0; i < state.realTimeAlarms.length; i++) {
      let alm = state.realTimeAlarms[i].alarm
      if (!state.realTimeAlarms[i].flag &&
        alm.happenTime === a.happenTime && alm.alarmContent === a.alarmContent &&
        alm.stationName === a.stationName && alm.trainNo === a.trainNo && 
        alm.workNo === a.workNo && alm.djNo === a.djNo) {
          state.realTimeAlarms[i].flag = true
          break
      }
    }
  }
}

const actions = {
  addAlarm({ commit }, alarm) {
    commit('ADD_ALARM', alarm)
  },
  addEvent({ commit }, alarm) {
    commit('ADD_EVENT', alarm)
  },
  setAlarmFlag({ commit }, alarm) {
    commit('SET_ALARM_FLAG', alarm)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
